package facilius.controller.action.usuario;

import facilius.model.ServiceLocator;
import facilius.model.pojo.Usuario;
import java.util.HashMap;
import java.util.Map;
import org.mentawai.core.BaseAction;

/**
 *
 * @author Graciano
 */
public class UsuarioShowFormAction extends BaseAction {

    @Override
    public String execute() throws Exception {
        String consequence = ERROR;
        String formAction = "UsuarioCreate.mtw";
        this.preload();
        try {
            Long id = input.getLong("id");
            if (id != null && id > 0){
                Usuario usuario = ServiceLocator.getUsuarioService().readById(id);
                output.setValue("email", usuario.getEmail());
                output.setValue("foto", usuario.getFoto());
                output.setValue("id", usuario.getId());
                output.setValue("login", usuario.getLogin());
                output.setValue("nome", usuario.getNome());
                output.setValue("senha", usuario.getSenha());
                output.setValue("tipo", usuario.getTipo());
                formAction = "UsuarioUpdate.mtw";
            }
            consequence = SUCCESS;
            output.setValue("formAction", formAction);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return consequence;
    }

    public void preload() throws Exception {
        Map<String, Object> criteria = new HashMap<String, Object>();
        Map<String, String> options = ServiceLocator.getUsuarioService().getOptions(criteria);
        output.setValue("usuarioTipoOptions", options);
    }
}
